/*获取当前页面url参数,并转换成json对象*/
function getUrlQuery() {
	var query = {}
	var params = window.location.search[1] ? window.location.search.slice(1).split("&") : [] //参数内容
	for (var i = 0; i < params.length; i++) { //将query转换为json
		var item = params[i].split("=");
		query[item[0]] = item[1]
	}
	return query
}

/*获取给定url参数,并转换成json对象*/
function getUrlAllParams(url) {
	var url = url
	var _pa = url.substring(url.indexOf('?') + 1),
		_arrS = _pa[1] ? _pa.split('&') : [],
		_rs = {}
	for (var i = 0; i < _arrS.length; i++) {
		var item = _arrS[i].split("=");
		_rs[item[0]] = item[1]
	}
	return _rs;
}

/* js对象转URL参数 */

function obj2Query(data) {
	var _result = [];
	for (var key in data) {
		var value = data[key];
		if (value.constructor == Array) {
			value.forEach(function(_value) {
				_result.push(key + "=" + _value);
			});
		} else {
			_result.push(key + '=' + value);
		}
	}
	return _result.join('&');
}

/*删除url指定参数，返回url*/
function delParamsUrl(url, name) {
	var baseUrl = url.split('?')[0] + '?';
	var query = url.split('?')[1];
	if (query.indexOf(name) > -1) {
		var obj = {}
		var arr = query.split("&");
		for (var i = 0; i < arr.length; i++) {
			arr[i] = arr[i].split("=");
			obj[arr[i][0]] = arr[i][1];
		};
		delete obj[name];
		var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g, "").replace(/\:/g, "=").replace(/\,/g, "&");
		return url
	} else {
		return url;
	}
}


// ES6
/*
 * @description: URL转对象及对象转URL
 * @author: HuiSir
 * @update: 20191115
 */

export const getUrlQuery = () => {
	var query = {}
	var params = window.location.search[1] ? window.location.search.slice(1).split("&") : [] //参数内容
	for (var i = 0; i < params.length; i++) { //将query转换为json
		var item = params[i].split("=");
		query[item[0]] = item[1]
	}
	return query
}

export const obj2Query = data => {
	var _result = [];
	for (var key in data) {
		var value = data[key];
		if (value.constructor == Array) {
			value.forEach(function(_value) {
				_result.push(key + "=" + _value);
			});
		} else {
			_result.push(key + '=' + value);
		}
	}
	return _result.join('&');
}

// 获取链接中query数据
export function getQueryInfo(key) {
	const queryString = window.location.search || window.location.href.split("?")[1];
	const SearchParams = new URLSearchParams(queryString)

	if (key) {
		return SearchParams.get(key)
	} else {
		const obj = {}
		for (const [key, value] of SearchParams) {
			obj[key] = value
		}
		return obj
	}
}